package com.huawei.hms.framework.network.grs;

import android.content.Context;
import android.text.TextUtils;
import com.huawei.hms.framework.common.Logger;
import com.huawei.hms.framework.network.grs.a.b;
import com.huawei.hms.framework.network.grs.b.e;
import com.huawei.hms.framework.network.grs.local.c;
import com.huawei.hms.framework.network.grs.local.model.CountryCodeBean;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.json.JSONObject;

public class a {
    private static final String a = a.class.getSimpleName();
    private GrsBaseInfo b;

    public a(GrsBaseInfo grsBaseInfo) {
        this.b = grsBaseInfo;
    }

    public static CountryCodeBean a(Context context, boolean z) {
        return new CountryCodeBean(context, z);
    }

    private String a(String str, String str2, b bVar) {
        Object a = com.huawei.hms.framework.network.grs.a.a.a(this.b, str, str2, bVar);
        if (!TextUtils.isEmpty(a)) {
            return a;
        }
        a = c.a().a(this.b, str, str2);
        if (!TextUtils.isEmpty(a)) {
            return a;
        }
        a = com.huawei.hms.framework.network.grs.local.b.a().a(this.b, str, str2);
        return TextUtils.isEmpty(a) ? null : a;
    }

    public static String a(String str, String str2, String str3) {
        String str4 = "";
        if (!TextUtils.isEmpty(str)) {
            try {
                str4 = new JSONObject(str).getJSONObject(str2).getString(str3);
            } catch (Throwable e) {
                Logger.w(a, "Method{getServiceNameUrl} query url from SP occur an JSONException", e);
            }
        }
        return str4;
    }

    private Map<String, String> a(String str, b bVar) {
        Map<String, String> a = com.huawei.hms.framework.network.grs.a.a.a(this.b, str, bVar);
        if (a != null && !a.isEmpty()) {
            return a;
        }
        a = c.a().a(this.b, str);
        if (a != null && !a.isEmpty()) {
            return a;
        }
        a = com.huawei.hms.framework.network.grs.local.b.a().a(this.b, str);
        return (a == null || a.isEmpty()) ? null : a;
    }

    public static Map<String, String> a(JSONObject jSONObject) {
        Map<String, String> hashMap = new HashMap(16);
        try {
            Iterator keys = jSONObject.keys();
            while (keys.hasNext()) {
                String obj = keys.next().toString();
                hashMap.put(obj, jSONObject.get(obj).toString());
            }
        } catch (Throwable e) {
            Logger.w(a, "getServiceUrls occur a JSONException", e);
        }
        return hashMap;
    }

    private void a(final String str, final Map<String, String> map, final IQueryUrlsCallBack iQueryUrlsCallBack) {
        e.a().a(this.b, new b(this) {
            final /* synthetic */ a d;

            public void a() {
                if (map == null || map.isEmpty()) {
                    iQueryUrlsCallBack.onCallBackFail(-3);
                } else {
                    iQueryUrlsCallBack.onCallBackSuccess(map);
                }
            }

            public void a(com.huawei.hms.framework.network.grs.b.c cVar) {
                Map b = a.b(cVar.a(), str);
                if (b != null && !b.isEmpty()) {
                    iQueryUrlsCallBack.onCallBackSuccess(b);
                } else if (map == null || map.isEmpty()) {
                    iQueryUrlsCallBack.onCallBackFail(-5);
                } else {
                    iQueryUrlsCallBack.onCallBackSuccess(map);
                }
            }
        });
    }

    public static Map<String, Map<String, String>> b(String str) {
        Map hashMap = new HashMap(16);
        if (TextUtils.isEmpty(str)) {
            Logger.v(a, "isSpExpire jsonValue is null.");
        } else {
            try {
                JSONObject jSONObject = new JSONObject(str);
                Iterator keys = jSONObject.keys();
                while (keys.hasNext()) {
                    String obj = keys.next().toString();
                    hashMap.put(obj, a(jSONObject.getJSONObject(obj)));
                }
            } catch (Throwable e) {
                Logger.w(a, "getServicesUrlsMap occur a JSONException", e);
            }
        }
        return hashMap;
    }

    public static Map<String, String> b(String str, String str2) {
        Map hashMap = new HashMap();
        if (TextUtils.isEmpty(str)) {
            Logger.v(a, "isSpExpire jsonValue is null.");
        } else {
            try {
                JSONObject jSONObject = new JSONObject(str).getJSONObject(str2);
                if (jSONObject == null) {
                    Logger.v(a, "getServiceNameUrls jsObject null.");
                } else {
                    Iterator keys = jSONObject.keys();
                    while (keys.hasNext()) {
                        String obj = keys.next().toString();
                        hashMap.put(obj, jSONObject.get(obj).toString());
                    }
                }
            } catch (Throwable e) {
                Logger.w(a, "Method{getServiceNameUrls} query url from SP occur an JSONException", e);
            }
        }
        return hashMap;
    }

    public String a() {
        com.huawei.hms.framework.network.grs.b.c a = e.a().a(this.b);
        return a == null ? "" : a.a();
    }

    public String a(String str, String str2) {
        b bVar = new b();
        String a = a(str, str2, bVar);
        if (bVar.a()) {
            Logger.v(a, "get unexpired cache localUrl{%s}", a);
            return a;
        }
        CharSequence a2 = a(a(), str, str2);
        if (TextUtils.isEmpty(a2)) {
            return a;
        }
        Logger.v(a, "get from remote server's remoteUrl {%s}", a2);
        return a2;
    }

    public Map<String, String> a(String str) {
        b bVar = new b();
        Map<String, String> a = a(str, bVar);
        if (bVar.a()) {
            return a;
        }
        Map<String, String> b = b(a(), str);
        return (b == null || b.isEmpty()) ? a : b;
    }

    public void a(String str, IQueryUrlsCallBack iQueryUrlsCallBack) {
        b bVar = new b();
        Map a = a(str, bVar);
        if (!bVar.a()) {
            a(str, a, iQueryUrlsCallBack);
        } else if (a == null || a.isEmpty()) {
            iQueryUrlsCallBack.onCallBackFail(-5);
        } else {
            iQueryUrlsCallBack.onCallBackSuccess(a);
        }
    }

    public void a(String str, String str2, IQueryUrlCallBack iQueryUrlCallBack) {
        b bVar = new b();
        final String a = a(str, str2, bVar);
        if (!bVar.a()) {
            final String str3 = str;
            final String str4 = str2;
            final IQueryUrlCallBack iQueryUrlCallBack2 = iQueryUrlCallBack;
            e.a().a(this.b, new b(this) {
                final /* synthetic */ a e;

                public void a() {
                    if (TextUtils.isEmpty(a)) {
                        iQueryUrlCallBack2.onCallBackFail(-3);
                    } else {
                        iQueryUrlCallBack2.onCallBackSuccess(a);
                    }
                }

                public void a(com.huawei.hms.framework.network.grs.b.c cVar) {
                    Object a = a.a(cVar.a(), str3, str4);
                    if (!TextUtils.isEmpty(a)) {
                        iQueryUrlCallBack2.onCallBackSuccess(a);
                    } else if (TextUtils.isEmpty(a)) {
                        iQueryUrlCallBack2.onCallBackFail(-5);
                    } else {
                        iQueryUrlCallBack2.onCallBackSuccess(a);
                    }
                }
            });
        } else if (TextUtils.isEmpty(a)) {
            iQueryUrlCallBack.onCallBackFail(-5);
        } else {
            iQueryUrlCallBack.onCallBackSuccess(a);
        }
    }
}
